package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.domain.TbUser;
import com.qf.domain.UserLogin;
import com.qf.domain.UserSearch;
import com.qf.service.TbUserService;
import com.qf.utils.JwtUtils;
import com.qf.utils.R;
import com.qf.utils.RedisUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@CrossOrigin(origins={"http://localhost:8081"},allowCredentials="true")
@RestController
@RequestMapping("/user")
@Slf4j
public class TbUserController {
    @Autowired
    private TbUserService userService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private JwtUtils jwtUtils;
    //登录
    @RequestMapping("/login")
    public R Login(@RequestBody UserLogin user){
        R login = userService.login(user);
        if (login.getCode()==200){
            return login;
        }
        return R.error("用户名或密码错误");

    }
    //登出
    @RequestMapping("/logout")
    public R logout(HttpServletRequest request){
        String token = jwtUtils.getToken(request);
        Claims claims = jwtUtils.parseJWT(token);
        Object uid = claims.get("uid");
        redisUtils.deleteObject("user:"+uid);
        return R.ok();
    }
    //注册
    @RequestMapping("/register")
    public R register(@RequestBody TbUser tbUser){
        if (tbUser.getUsername()==null||tbUser.getPassword()==null){
            return R.error("用户名或密码不能为空");
        }
        return userService.addUser(tbUser);
    }
    //根据ID查询用户信息
    @RequestMapping("/getUserById")
    public R getUserById(Integer id){
        if (id==null){
            return R.error("请输入用户ID");
        }
        return userService.getUserById(id);
    }
    //修改用户信息
    @RequestMapping("/updateUserById")
    public R updateUserById(@RequestBody TbUser tbUser){
        if(tbUser.getId()==null){
            return R.error("请输入用户ID");
        }
        return userService.updateUserById(tbUser);
    }
    //查询所有用户信息
    @RequestMapping("/getAllUser")
    public R getAllUser(){
        return userService.getAllUser();
    }
    //模糊查询
    @RequestMapping("/getUserBySearch")
    public R getUserBySearch(@RequestBody UserSearch userSearch){
        System.out.println(userSearch);
        return userService.getUserBySearch(userSearch);
    }
    //分页查询
    @RequestMapping("/getUserByPage")
    public R getUserByPage( UserSearch userSearch){
        System.out.println(userSearch);
        PageHelper.startPage(userSearch.getPageNumber(),userSearch.getPageSize());
        ArrayList<TbUser> tbUsers = (ArrayList<TbUser>) userService.getUserBySearch(userSearch).getData();
        PageInfo<TbUser> pageInfo = new PageInfo<>(tbUsers);
        return R.ok(pageInfo);

    }
    //根据ID删除用户
    @RequestMapping("/deleteUserById")
    public R deleteUserById(Integer id){
        if (id==null){
            return R.error("请输入用户ID");
        }
        return userService.deleteUserById(id);
    }
    //批量删除选中用户
    @RequestMapping("/deleteUsers")
    public R deleteUsers(@RequestParam ArrayList<Integer> ids){
        if (ids==null){
            return R.error("请选择要删除的用户");
        }
        return userService.deleteUsers(ids);
    }


}
